<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class
 *
 * @author ������
 */
class View{
//public function getPager(Pager $pager) {
//    
//if($pager->getNumOfPages()==1)    return;
//
//$out = '';
//$out.='<div class="pagination span6 block-center"><ul>';
//if($pager->getCurrentPage()>0)
//    $out.= '<li><a href="?page='.($pager->getCurrentPage()-1).'&pager_id='.$pager->getPagerId().'"">Пред.</a></li>';
//for($i=0; $i<$pager->getNumOfPages(); $i++)
//{
//    $out.= '<li class="'.($pager->getCurrentPage()==$i?'active':'').'"><a href="?page='.$i.'&pager_id='.$pager->getPagerId().'"">'.($i+1).'</a></li>';
//}
//if(($pager->getCurrentPage()+2)<=$pager->getNumOfPages())
//    $out.= '<li><a href="?page='.($pager->getCurrentPage()+1).'&pager_id='.$pager->getPagerId().'">След.</a></li>';
//
//$out.='</ul></div>';
//return $out;
//}
    private $context;
    private $registry;
    
    public function __construct($context) {
        $this->context = $context;
        $this->registry = Registry::getInstance();
    }

    //TODO: вызвать getHTML()
    public function __call($name, $arguments) {
        if (preg_match("/^get([a-zA-Z0-9_-]+)$/ui", $name, $matches)) {
            return $this->getHTML(strtolower($matches[1]), (!empty($arguments)?$arguments[0]:array()));
        }
    }
    
    public function getHTML($template_name, $args) {
        $theme = THEME;
        $relative_theme_path='html/views';

        $args = array('args'=>$args);      
        $html = $this->registry->themeunit->getHTMLFromPath($this->context->getCurrentAction(), $theme, $relative_theme_path, $template_name, $args,'view');
        return $html;
    }

       public static function form_ex($params) {
        $hidden_fields = &$params['hidden_fields'];
        $hidden_fields['action'] = $params['action'];
        $hidden_fields['token'] = generateCode(16);
        
        if(session_status()!=PHP_SESSION_ACTIVE) session_start ();
        $_SESSION['form'][$params['action']]['token'] = $hidden_fields['token'];
        $out = '<form '.(isset($params['name'])?'name="'.$params['name'].'" ':'').''.(isset($params['id'])?'id="'.$params['id'].'" ':'').'action="' . (isset($params['form_action'])?$params['form_action']:'') . '" method="POST"'.(isset($params['multipart']) && $params['multipart'] == true?" enctype=\"multipart/form-data\"":"").'>' . View::array_to_form_fields($hidden_fields);
        $out .= $params['body'].(isset($params['buttons'])?$params['buttons']:'').'</form>';
        return $out;
    }
       public static function form($body, $target_action, $hidden_fields, $action, $buttons_html, $multi=false) {
        $hidden_fields['action'] = $action;
        $hidden_fields['token'] = generateCode(16);
        if(session_status()!=PHP_SESSION_ACTIVE) session_start ();
        $_SESSION['form'][$action]['token'] = $hidden_fields['token'];
        $out = '<form action="' . $target_action . '" method="POST"'.($multi?" enctype=\"multipart/form-data\"":"").'>' . View::array_to_form_fields($hidden_fields);
        $out.=$body.$buttons_html;
        return $out;
    }
    
   public static function array_to_form_fields(&$data) {
       $out = '';
       
       foreach ($data as $key => $value) {
           $out.='<input type="hidden" name="'.$key.'" value="'.$value.'">';
       }
       return $out;
   }
    public function add($data) {
        
        $this->data = array_merge_recursive($this->data, $data);
    }
    
}
?>
